package org.his.controller.basicdata.huiyuan;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.his.dao.CardTranInfoDao;
import org.his.dao.MemberCardlnfoDao;
import org.his.pojo.huiyuan.CardTranInfo;
import org.his.pojo.huiyuan.MemberCardInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/menber")
public class MemberCardInfoController {

    @Autowired
    private MemberCardlnfoDao memberCardlnfoDao;

    @Autowired
    private CardTranInfoDao cardTranInfoDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "basicdata/huiyuan/index";
    }

    @RequestMapping("/form")
    public String form() throws Exception {
        return "basicdata/huiyuan/form";
    }


    @RequestMapping("/chongzhi")
    public String chongzhi() throws Exception {
        return "basicdata/huiyuan/chongzhiform";
    }


    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, MemberCardInfo condition) throws Exception{
        Map<String, Object> map = new HashMap<>();
        Page<MemberCardInfo> pager = PageHelper.startPage(page, limit);

        map.put("data", memberCardlnfoDao.getList(condition));
        map.put("count", pager.getTotal());
        map.put("code", 0);
        map.put("msg", null);
        return map;
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public Map<String,Object> batchDelete(Integer ids) throws Exception{
        Map<String,Object> map = new HashMap<>();
        cardTranInfoDao.delete(ids);
        memberCardlnfoDao.delete(ids);
        return map;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(MemberCardInfo memberCardInfo) throws Exception{
        Map<String,Object> map = new HashMap<>();
        memberCardlnfoDao.add(memberCardInfo);

        CardTranInfo cardTranInfo = new CardTranInfo();
        cardTranInfo.setCardMemberId(memberCardInfo.getCardMemberId());
        cardTranInfoDao.add(cardTranInfo);

        return map;
    }


    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(MemberCardInfo memberCardInfo) throws Exception{
        Map<String,Object> map = new HashMap<>();
        memberCardlnfoDao.update(memberCardInfo);

        return map;
    }



    @RequestMapping("/get")
    @ResponseBody
    public MemberCardInfo get(Integer cardMemberId) throws Exception{
        return memberCardlnfoDao.get(cardMemberId);
    }

    @RequestMapping("/dongjie")
    @ResponseBody
    public Map<String,Object> dongjie(Integer carType ,Integer cardMemberId) throws Exception{
        Map<String,Object> map = new HashMap<>();

        memberCardlnfoDao.dongjie(carType,cardMemberId);
        cardTranInfoDao.dongjie(carType,cardMemberId);

        return map;
    }

}
